#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fstream>
#include "json.hpp"
#include "Struct.h"
#include "Timer.hpp"

using namespace std;

int main(int argc, char** argv)
{
	string USER="user.json";
	if(argc>1)
		USER=argv[1];
	ifstream in;
	in.open(USER.c_str());
    string content((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
	in.close();
	nlohmann::json config = nlohmann::json::parse(content);

	const char* fn = config["TRADE_RECORD"].get<string>().c_str();
	int ft = open(fn,O_RDONLY);
	Order* orders = (Order*)mmap(NULL,sizeof(Order)*OrderCapacity, PROT_READ,MAP_SHARED, ft, 0);
	close(ft);
	int id= 0;
	while(1)
	{
		Order* o = orders+id;
		if(o->md_nano == 0)
			break;
		id++;
		fprintf(stdout, "%4s id:%3d %s +%5.0fus +%5.0fus +%5.0fus +%5.0fus %s %c %c %.0f %.0f %2d/%2d\n", o->sent_nano?"sent":"",o->id, Timer::nano2str(o->md_nano).c_str(), 
			(o->process_nano-o->md_nano)/1e3,
			(o->order_nano-o->process_nano)/1e3,
			(o->sent_nano- o->order_nano)/1e3,
			(o->last_trade_nano-o->sent_nano)/1e3, 
			o->symbol, o->direction, o->offset, 
			o->order_price,o->deal_price, o->deal_volume,o->target_volume);
	}
	return 0;
}
